package com.cy.pj.sys.service.impl;

import com.cy.com.cy.pj.exception.ServiceException;
import com.cy.pj.sys.dao.SysMenuDao;
import com.cy.pj.sys.pojo.Node;
import com.cy.pj.sys.pojo.SysMenu;
import com.cy.pj.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuDao sysMenuDao;

    //菜单管理
    @Override
    public List<Map<String, Object>> findObjects() {
        List<Map<String,Object>> list=sysMenuDao.findObjects();
        if (list==null || list.size()==0)
            throw new ServiceException("没有对应信息");
        return list;
    }

    //上级菜单
    @Override
    public List<Node> findZtreeMenuNodes() {
        return sysMenuDao.findZtreeMenuNodes();
    }

    //添加数据
    @Override
    public int saveObject(SysMenu sysMenu) {
        if (sysMenu==null)
            throw new IllegalArgumentException("保存对象不能为空");
        if (StringUtils.isEmpty(sysMenu.getName()))
            throw new IllegalArgumentException("菜单不能为空");
        int rows=sysMenuDao.insertObject(sysMenu);
        return rows;
    }

    //修改菜单数据
    @Override
    public int updateObject(SysMenu sysMenu) {
        if (sysMenu==null)
            throw new ServiceException("保存对象不能为空");
        if(StringUtils.isEmpty(sysMenu.getName()))
            throw new ServiceException("菜单不能为空");
        int rows=sysMenuDao.updateObject(sysMenu);
        if (rows==0)
            throw new ServiceException("该记录可能已存在");
        return rows;
    }

}
